//返回
$('.xzh-nav-right').on('click', function() {
    location.href = 'teacherPage.html';
});

//取消
$('.xzh-fix-left').on('click', function() {
    location.href = 'teacherPage.html';
});

//获取本地编辑id
let teacherId = localStorage.getItem('teacherId');
console.log(teacherId);

//确定
$('.xzh-fix-right').on('click', function () {
    let teacherName = $('.teacher-name').val();
    let teacherTel = $('.teacher-tel').val();
    let sectionId = $('#teacher-bumen').val();
    let teacherStanding = $('#teacher-gongling').val();
    let teacherEmail = $('#teacher-ea').val();
    let sexId = $('#teacher-sex').val();
    let educationId = $('#teacher-jiaoyu').val();
    let teacherEntry = $('#demoTest').val();

    let obj = {
        teacherId,
        teacherName,
        teacherTel,
        sectionId,
        teacherStanding,
        teacherEmail,
        sexId,
        educationId,
        teacherEntry
    }

    console.log(obj);

    //判断是否有输入内容
    if(!teacherName || !teacherTel || !sectionId || !teacherStanding || !teacherEmail || !sexId || !educationId || !teacherEntry) {
        return alert('内容不能为空');
    }

    //姓名格式判断
    let name = /^[\u4E00-\u9FA5A-Za-z]+$/;
    if(!name.test(teacherName)) {
       return alert('姓名格式错误');
    }

    //邮箱格式判断
    var email = /^[A-Za-z0-9]+([_\.][A-Za-z0-9]+)*@([A-Za-z0-9\-]+\.)+[A-Za-z]{2,6}$/;
    if(!email.test(teacherEmail)) {
        return alert('邮箱格式错误');
    }
    
    //电话号码格式判断
    var tel = /^((\+|00)86)?1((3[\d])|(4[5,6,7,9])|(5[0-3,5-9])|(6[5-7])|(7[0-8])|(8[\d])|(9[1,8,9]))\d{8}$/;
    if(!tel.test(teacherTel)) {
        return alert('电话号码格式错误');
    }

    $.ajax ({
        url: '/teachers/edit',
        method: 'POST',
        data: obj
    })

    .done (function (res){
        console.log('编辑成功:',res);
        
        location.href = 'teacherPage.html';

    })
    .fail (function (err) {
        console.log('编辑失败:',err);
        
    })
})

//数据请求
function insertOneTeacher() {
    $.ajax({
        url: '/teachers/getOneTeacher',
        method: 'POST',
        data: {
            teacherId: teacherId
        }
    })
    .done(function (res) {
        console.log('请求成功：', res);
        let teacherName = $('.teacher-name').val(res.data.teacherName);
        let teacherTel = $('.teacher-tel').val(res.data.teacherTel);
        let sectionId = $('#teacher-bumen').val(res.data.sectionId);
        let teacherStanding = $('#teacher-gongling').val(res.data.teacherStanding);
        let teacherEmail = $('#teacher-ea').val(res.data.teacherEmail);
        let sexId = $('#teacher-sex').val(res.data.sexId);
        let educationId = $('#teacher-jiaoyu').val(res.data.educationId);
        let teacherEntry = $('#demoTest').val(res.data.teacherEntry);
    })
    .fail(function (err) {
        console.log('请求失败：',err);
    })
};

insertOneTeacher();

//获取点击目标id的数据
// var bianjiIndex;

// function bianji() {
//     for(var i = 0; i < teachers.length; i++) {
//         if(teachers[i].id == teacherId) {
//             bianjiIndex = i;
//         }
//     }
// }

// bianji();
// console.log(teachers[bianjiIndex]);

// window.onload = function() {
//     console.log(teachers);
//     $('.teacher-name').val(teachers[bianjiIndex].name);
//     $('.teacher-tel').val(teachers[bianjiIndex].tel);
//     $('#teacher-bumen').val(teachers[bianjiIndex].section);
//     $('#teacher-gongling').val(teachers[bianjiIndex].standing);
//     $('#teacher-ea').val(teachers[bianjiIndex].email);
//     $('#teacher-sex').val(teachers[bianjiIndex].sex);
//     $('#teacher-jiaoyu').val(teachers[bianjiIndex].teach);
//     $('#demoTest').val(teachers[bianjiIndex].entry); 
// }

// $('.xzh-fix-right').on('click', function() {
//     teachers[bianjiIndex].name = $('.teacher-name').val();
//     teachers[bianjiIndex].tel = $('.teacher-tel').val();
//     teachers[bianjiIndex].section = $('#teacher-bumen').val();
//     teachers[bianjiIndex].standing = $('#teacher-gongling').val();
//     teachers[bianjiIndex].email = $('#teacher-ea').val();
//     teachers[bianjiIndex].sex = $('#teacher-sex').val();
//     teachers[bianjiIndex].teach = $('#teacher-jiaoyu').val();
//     teachers[bianjiIndex].entry = $('#demoTest').val();

//     //判断是否有输入内容
//     if(!teachers[bianjiIndex].name || !teachers[bianjiIndex].tel || !teachers[bianjiIndex].section || !teachers[bianjiIndex].standing || !teachers[bianjiIndex].email || !teachers[bianjiIndex].sex || !teachers[bianjiIndex].teach || !teachers[bianjiIndex].entry) {
//         return alert('内容不能为空');
//      }
     
//     //  判断格式
//     //  电话
//      var telPat = /^[0-9]{7,7}$/;
//      if(!telPat.test(teachers[bianjiIndex].tel)) {
//          return alert('电话格式不正确');
//      }
     
//      //邮箱
//      var emailPat = /^[a-zA-Z]{16,16}$/i;
//      if(!emailPat.test(teachers[bianjiIndex].email)) {
//          return alert('邮箱格式不正确');
//      }

//     localStorage.setItem('teachers',JSON.stringify(teachers));

//     location.href = 'teacherPage.html';

//     bianjixuanran(teachers);
// });

// bianjixuanran(teachers);


// function bianjixuanran() {
//     var teachers = JSON.parse(localStorage.getItem('teachers'));
    
//     for(var i = 0; i < teachers.length; i++) {
//         var tr = `
//             <tr class="xzh-td1">
//                 <td><input type="checkbox"></td>
//                 <td>${teachers[i].name}</td>
//                 <td>${teachers[i].standing}</td>
//                 <td>${teachers[i].section}</td>
//                 <td>${teachers[i].sex}</td>
//                 <td>${teachers[i].teach}</td>
//                 <td>${teachers[i].tel}</td>
//                 <td>${teachers[i].email}</td>
//                 <td>${teachers[i].entry}</td>
//                 <td>
//                 <p class='chakan' data-id=${teachers[i].id}>查看</p>
//                 <P class='bianji' data-id=${teachers[i].id}>编辑</P>
//                 </td>
                
//             </tr>
//         `;

//         $('.xzh-teacher-tbody').append($(tr));
//     }
// }